package com.excellence.service.impl;

import com.excellence.mapper.KnowledgeBaseMapper;
import com.excellence.pojo.KnowledgeBase;
import com.excellence.service.KnowledgeBaseService;
import com.excellence.utils.SaveFilesPath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;


/**
 * @author 徐瑞琪
 */
@Service
public class KnowledgeBaseServiceImpl implements KnowledgeBaseService {

    @Autowired
    KnowledgeBaseMapper knowledgeBaseMapper;

    //新增方法,图片可以不传，传一个或多个
    @Override
    public void insert(KnowledgeBase record, List<MultipartFile> files) {
        if (files != null) {
            record.setKnImage(SaveFilesPath.saveFilesPath(files));
        }
        Date date = new Date();
        record.setCreateTime(date);
        record.setUpdateTime(date);
        record.setDeleted((short) 0);
        knowledgeBaseMapper.insert(record);
    }

    //根据ID查知识
    @Override
    public KnowledgeBase selectById(Integer knId) {
        return knowledgeBaseMapper.selectById(knId);
    }

    //根据知识类型查知识
    @Override
    public List<KnowledgeBase> selectByKnType(String knType) {
        return knowledgeBaseMapper.selectByKnType(knType);
    }

    //查看所有知识
    @Override
    public List<KnowledgeBase> selectAll() {
        return knowledgeBaseMapper.selectAll();
    }

    //根据ID删除数据
    @Override
    public void deleteById(Integer id) {
        Date date = new Date();
        KnowledgeBase record = new KnowledgeBase();
        record.setKnId(id);
        record.setUpdateTime(date);
        record.setDeleted((short) 1);
        knowledgeBaseMapper.update(record);
    }

    //修改知识
    @Override
    public void updateHaveFile(KnowledgeBase record, List<MultipartFile> files) {
        if (files != null) {
            record.setKnImage(SaveFilesPath.saveFilesPath(files));
        }
        record.setUpdateTime(new Date());
        knowledgeBaseMapper.update(record);

    }

}
